package com.ws.dn.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ws.dn.api.user.UserService;
import com.ws.dn.mapper.user.entity.SysUser;
import com.ws.dn.mapper.user.entity.SysUserInfo;
import com.ws.dn.mapper.user.mapper.SysUserMapper;
import com.ws.dn.mapper.user.param.RegisterUser;
import com.ws.dn.mapper.user.param.UpdateUser;
import com.ws.dn.mapper.user.param.UpdateUserInfo;
import com.ws.rain.core.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Reference(loadbalance = "roundrobin",timeout = 1000)
    private UserService userService;

    @Autowired
    private SysUserMapper userMapper;

    @PostMapping("/success")
    @ApiOperation("登录成功，回调接口")
    public R loginSuccess() {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = principal.getUsername();
        EntityWrapper<SysUser> ew = new EntityWrapper<>();
        ew.eq("username",username);
        SysUser sysUser = userMapper.selectList(ew).get(0);
        sysUser.setPassword("[PROTECT]");
        return R.data(sysUser);
    }

    @PostMapping("/register")
    @ApiOperation("注册用户")
    public R register(RegisterUser user) {
       SysUser sysUser = userService.register(user);
        return R.data(sysUser);
    }

    @PostMapping("/update")
    @ApiOperation("修改用户个人信息")
    public R updateUserInfo(UpdateUserInfo user) {
       SysUserInfo sysUser = userService.updateUserInfo(user);
        return R.data(sysUser);
    }

//    @PostMapping("/update/pwd")
//    @ApiOperation("修改用户密码")
//    public R updatePwd(UpdateUser user) {
//        String str = userService.updateUser(user);
//        return R.data(str);
//    }

    @PostMapping("/update/role")
    @ApiOperation("修改用户角色")
    public R updateRole(UpdateUser user) {
        String str = userService.updateUser(user);
        return R.data(str);
    }


}
